package cn.unknowpupil.controller.system;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.common.security.dto.LoginUser;
import cn.unknowpupil.common.security.utils.SecurityUtils;
import cn.unknowpupil.domain.system.SysRole;
import cn.unknowpupil.service.system.ISysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Created at 2020-10-21 by  w10g <br>
 * Initialize SysRole  ... <br>
 * 角色信息表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-角色信息表模块"}, value = "SysRole")
@RestController
@RequestMapping(value = "admin/sys-role")
public class SysRoleController {

    @Resource
    private ISysRoleService SysRoleService;

    /**
     * 新增
     *
     * @param SysRole {@link SysRole}  角色信息表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "create", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Validated  @RequestBody SysRole SysRole) {

        // 业务逻辑
        boolean created = SysRoleService.create(SysRole);
        if (created) {
            return ResultResponse.success("创建成功");
        } else {
            return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param role {@link SysRole}  角色信息表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "update", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysRole role) {

        // 业务逻辑
        boolean update = SysRoleService.update(role);
        if (update) {
            // 更新缓存用户权限
//            LoginUser loginUser = SecurityUtils.getLoginUser();

            return ResultResponse.success("修改成功");
        } else {
            return ResultResponse.buildFailure("修改角色'" + role.getRoleName() + "'失败，请联系管理员");
        }

    }


    /**
     * 获取
     *
     * @param SysRoleId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{SysRoleId}")
    public ResultResponse get(@PathVariable("SysRoleId") Long SysRoleId) {
        SysRole SysRole = SysRoleService.info(SysRoleId);
        return ResultResponse.buildSuccess(SysRole);
    }

    @ApiOperation("系统：状态修改")
    @Log(title = "状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("change/status")
    public ResultResponse changeStatus(@RequestBody SysRole role) {
        SysRoleService.checkRoleAllowed(role);
        boolean b = SysRoleService.updateById(role);
        if (b) {
            return ResultResponse.success("成功");
        } else {
            return ResultResponse.buildFailure();
        }
    }

    @ApiOperation("系统：修改保存数据权限")
    @Log(title = "修改保存数据权限", businessType = BusinessType.UPDATE)
    @PutMapping("change/scope")
    public ResultResponse dataScope(@RequestBody SysRole role) {
        SysRoleService.checkRoleAllowed(role);
        boolean row = SysRoleService.authDataScope(role);
        if (row) {
            return ResultResponse.success("成功");
        } else {
            return ResultResponse.buildFailure();
        }
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted = SysRoleService.delete(ids);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：分页")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysRole>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysRole SysRole) {
        IPage<SysRole> page = SysRoleService.page(current, size, SysRole);
        return ResultResponse.buildSuccess(page);
    }

    @ApiOperation("系统：获取角色选择框列表")
    @GetMapping("/option/select")
    public ResultResponse optionSelect() {
        return ResultResponse.buildSuccess(SysRoleService.selectRoleAll());
    }


}
